<template>
	<view>
		<view class="mendlb" v-for="(item,index) in allData" :key="index" @click="gomendxx(item.id)">
			<image class="mbtoub" v-if="item.image.length==0" src="../../static/avatar.jpg" mode="aspectFill"></image>
			<image class="mbtoub" v-else :src="imgUrl+item.image[0]" mode="aspectFill"></image>
			<view class="youbin">
				<view class="mendmc">{{item.mdname}}</view>
				<view class="hengxx">
					<view class="yeji">状态:{{item.state}}</view>
					<view class="jind">距离:{{item.juli}}</view>
					<view class="yug">员工:{{item.yuangong.length}}人</view>
				</view>
				<view class="dizhi">
					<image class="dizhtb" src="../../static/dizhi.png"></image>
					<view>{{item.address}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: this.$imgUrl,
				yg_id:'',
				user:null,
				sex:'',
				name:'',
				touxiang:'',
				dqjs:'',
				mendian_id:'',
				area:'',
				getDataFlag:false,
				latitude:'',
				longitude:'',
				province:'',
				city:'',
				allData:[]//
			}
		},
		onLoad() {
			var that = this;
			uni.getStorage({
				key: 'user_key',
				complete: function(res) {
					// console.log(666,res)
					that.yg_id = res.data.id;
					that.user = res.data;
					that.sex = res.data.sex;
					that.name = res.data.name;
					that.touxiang = res.data.touxiang;
					that.dqjs = res.data.role;
					that.mendian_id = res.data.mendian;
					that.getLocation();
				}
			})
		},
		onReady() {
			if (this.dqjs == 3) {
				//区域获取所有门店
				this.getAreaStoreList()
			}
			if (this.dqjs == 4) {
				//城市获取所有门店
				this.getCityStoreList()
			}
			if (this.dqjs == 5) {
				//老板角色查询所有门店列表
				this.getStoreList();
			}
		
		},
		methods: {
			gomendxx(id){
				uni.navigateTo({
					url:'../yuanggl/yuanggl?mendian_id='+id
				})
			},
			//老板获取所有的门店列表
			getStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: 0,
						longitude: 0
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555,res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
						let allData1 = res.data.data;
						_that.allData=[];
						if (allData1.length > 11) {
							_that.allData = allData1.splice(10);
						} else {
							_that.allData = allData1;
						}
			
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//市级经理代表获取旗下的所有门店
			getCityStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: _that.latitude,
						longitude: _that.longitude,
						city: _that.city
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555, res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
							let allData1 = res.data.data;
							_that.allData=[];
							if (allData1.length > 11) {
								_that.allData = allData1.splice(10);
							} else {
								_that.allData = allData1;
							}
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//区域经理代表获取旗下的所有门店
			getAreaStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: _that.latitude,
						longitude: _that.longitude,
						area: _that.area
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555,res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
							_that.allData=[];
							let allData1 = res.data.data;
							if (allData1.length > 11) {
								_that.allData = allData1.splice(10);
							} else {
								_that.allData = allData1;
							}
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//获取定位当前城市
			async getLocation() {
				const QQMapWX = new qqmapsdk({
					key: 'TFXBZ-WKMK3-QHG3Q-YF7IY-SLEIZ-6CFHQ'
				});
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: (res) => {
						console.log("获取经纬度成功");
						this.latitude = res.latitude;
						this.longitude = res.longitude;
					},
					fail: () => {
						console.log("获取经纬度失败");
					},
					complete: () => {
						// 解析地址
						QQMapWX.reverseGeocoder({
							location: {
								latitude: this.latitude,
								longitude: this.longitude
							},
							success: function(res) {
								console.log("解析地址成功");
								// 省
								let province = res.result.ad_info.province;
								this.province = province;
								// 市
								let city = res.result.ad_info.city;
								this.city = city
								// console.log(province);
								// console.log(city);
							},
							fail: function(res) {
								uni.showToast({
									title: '定位失败',
									duration: 2000,
									icon: "none"
								})
								console.log(res);
							},
							complete: function(res) {
								console.log(res);
								this.area = res.result.address_component.district;
								this.getDataFlag = true;
							}
						})
					}
				})
			
			}
			
		}
	}
</script>
<style>
	page{
		background: #f0f0f0;
	}
</style>
<style lang="scss" scoped>
	.mendlb{
		width: 690rpx;
		margin: 30rpx 30rpx 0 30rpx;
		background: #ffffff;
		display: flex;
		align-items: center;
		border-radius: 20rpx;
		.mbtoub{
			width: 150rpx;
			height: 150rpx;
			border-radius: 20rpx 0 0 20rpx;
		}
		.youbin{
			margin-left: 10rpx;
			.mendmc{
				font-weight: bold;
			}
			.hengxx{
				display: flex;
				align-items: center;
				font-size: 25rpx;
				margin: 10rpx 0;
				.yeji{
					padding: 5rpx 10rpx;
					background: #2979FF;
					color: #fff;
					margin-right: 10rpx;
					border-radius: 10rpx;
				}
				.jind{
					padding: 5rpx 10rpx;
					background: #ff007f;
					color: #fff;
					margin-right: 10rpx;
					border-radius: 10rpx;
				}
				.yug{
					padding: 5rpx 10rpx;
					background: #00aaff;
					color: #fff;
					margin-right: 10rpx;
					border-radius: 10rpx;
				}
			}
			.dizhi{
				display: flex;
				align-items: center;
				font-size: 25rpx;
				color: #303133;
				.dizhtb{
					width: 25rpx;
					height: 25rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
</style>
